#include "data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

int main(void)
{
    sqlite3 *db;
    struct data d;
    struct value v;
    int i;
    if(sqlite3_open("test2.db",&db) != SQLITE_OK){
        perror("sqlite3_open");
        exit(-1);
    }

    data_init(&d,db);

    d.sql = "INSERT INTO garage VALUES (\"粤B 11111\",1620455272,0,1);";
    data_query(&d);
    data_clear(&d);

    d.sql = "INSERT INTO garage VALUES (\"粤A 22222\",16204666666,0,1);";
    data_query(&d);
    data_clear(&d);

    d.sql = "INSERT INTO garage VALUES (\"粤C 33333\",16204677777,0,1);";
    data_query(&d);
    data_clear(&d);

    d.sql = "SELECT * FROM garage;";
    data_query(&d);
    
    while(data_get(&d,&v)){
        for(i = 0;i < v.num; i++)
            printf("%s ",v.data[i]);
        printf("\n");
    }
    data_clear(&d);

    d.sql = "SELECT * FROM garage WHERE id = \"粤B 11111\" AND state = 1;";
    data_query(&d);
    data_get(&d,&v);
    for(i = 0;i < v.num; i++)
        printf("%s ",v.data[i]);
    printf("\n");
    data_clear(&d);

    d.sql = "SELECT * FROM garage WHERE id = \"粤A 22222\" AND state = 1;";
    data_query(&d);
    data_get(&d,&v);
    for(i = 0;i < v.num; i++)
        printf("%s ",v.data[i]);
    printf("\n");
    data_clear(&d);

    d.sql = "UPDATE garage SET state = 0 WHERE id = \"粤C 33333\";";
    data_query(&d);
    data_clear(&d);

    d.sql = "SELECT * FROM garage WHERE id = \"粤C 33333\";";
    data_query(&d);
    data_get(&d,&v);
    for(i = 0;i < v.num; i++)
        printf("%s ",v.data[i]);
    printf("\n");
    data_clear(&d);

    d.sql = "DELETE FROM garage WHERE id = \"粤C 33333\";";
    data_query(&d);
    data_clear(&d);

    d.sql = "SELECT * FROM garage;";
    data_query(&d);
    
    while(data_get(&d,&v)){
        for(i = 0;i < v.num; i++)
            printf("%s ",v.data[i]);
        printf("\n");
    }
    data_clear(&d);

    d.sql = "DELETE FROM garage;";
    data_query(&d);
    data_clear(&d);

    sqlite3_close(db);
    return 0;
}